/*
 * Nate Waldrop
 * CS 3230
 * Assignment 3 - Address Book
 */
package Assignment03;

import java.util.*;
import java.io.*;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Main {

	/**
	 * @param args
	 */
	static String[] FirstNameList = { "Lofman", "Ezdebski", "Dorvillier",
			"Dziurawiec", "Griffel", "Gravelin", "Ermita", "Hannula", "Lawis",
			"Evanski", "Crutchev", "Bhairo", "Chilinskas", "Leutwiler",
			"Hearston", "Mcginnity", "Bordi", "Meggett", "Bernaudo", "Kronau",
			"Colmer", "Myntti", "Kopicko", "Lisbey", "Laroia", "Karro",
			"Curls", "Hargest", "Hercman", "Cheon", "Noneman", "Packineau",
			"Letchaw", "Myking", "Felico", "Hardinson", "Millbrand",
			"Derubeis", "Lofman", "Ciampanella", "Gretzner", "Arnsberger",
			"Merithew", "Dedek", "Kabigting", "Dalponte", "Cezil", "Colopy",
			"Gallagos", "Navarrate", "Fetrow", "Muschick", "Cabanes", "Batkin",
			"Arrellin", "Gummersall", "Deluccia", "Nipps", "Aseng", "Buchs" };
	static String[] LastNameList = { "JAROD", "BOYCE", "FRANKLYN", "JAE",
			"WES", "LONG", "HYMAN", "RUDOLF", "LYNWOOD", "GAYLORD", "TUAN",
			"FRANCESCO", "CORTEZ", "ARLEN", "FRANCESCO", "JACINTO", "ELDEN",
			"ARLEN", "KASEY", "MODESTO", "FAUSTO", "MICHALE", "TONEY", "ROLF",
			"TONEY", "LEIF", "ARLEN", "HOSEA", "ELLSWORTH", "NATHANAEL",
			"GASTON", "HAYDEN", "HAI", "HAYDEN", "JERAMY", "HYMAN", "ELDEN",
			"LAUREN", "PATRICIA", "BOYCE", "SANTO", "BUCK", "CEDRICK", "SHON",
			"ANTIONE", "ERASMO", "JUDE", "LONG", "WALTON", "MICHAL", "JERRELL",
			"MOSE", "LUCIUS", "SHON", "FELTON", "ARLIE", "LEANDRO", "EZEQUIEL",
			"BENEDICT" };

	static Random r = new Random();

	public static void main(String[] args) {

		AddressBook ab = new AddressBook();
		for (int i = 0; i < 5; i++) {
			// Randomly create a person from the list above, and give them a
			// random birthday
			Calendar c = new GregorianCalendar(
					(r.nextInt(2011 - 1970 + 1) + 1970), r.nextInt(11),
					r.nextInt(28));
			Person p = new Person(getRandomName(true), getRandomName(false),
					c.getTime());
			Address a = new Address("123 My House", "My City", "My State",
					12345);

			// Add more than 500 -show ability to track address book size REQ 4A
			if (ab.AddAddressBookItem(new AddressBookItem(p, a))) {
				System.out.println("Successfully Added Entry # "
						+ ab.GetAddressBookSize());
			} else
				System.out.println("Unable to add entry");

		}

		// Demonstrate address book capabilities
		String currentFirst, currentLast;
		// REQ 4B Search for person by name
		// Because of the random nature of the list, I will loop and try to find
		// 10 random combinations

		System.out.println("***********************************\n");
		for (int i = 0; i < 10; i++) {
			currentFirst = getRandomName(true);
			currentLast = getRandomName(false);
			System.out.println("\nAttempting to find: " + currentFirst + " "
					+ currentLast);
			ab.SearchByName(currentFirst, currentLast);
		}

		// REQ 4C Print the address, phone, and date of birth of a given person
		// Again, looping to find using random list
		System.out.println("***********************************\n");
		for (int i = 0; i < 10; i++) {
			currentFirst = getRandomName(true);
			currentLast = getRandomName(false);
			System.out.println("\nAttempting to print: " + currentFirst + " "
					+ currentLast);
			ab.PrintEntryByName(currentFirst, currentLast);
		}

		// REQ 4D Print the names of all people with the same last name
		for (int i = 0; i < 3; i++) {
			System.out.println("***********************************\n");
			currentLast = getRandomName(false);
			System.out.println("Listing all individuals with the last name: "
					+ currentLast);
			ab.PrintEntriesByLastName(currentLast);
		}

		XStream xstream = new XStream();
		xstream.alias("addressbook", AddressBook.class);
		String xml = xstream.toXML(ab);
		System.out.println(xml);

		XStream readIn = new XStream(new DomDriver());
		readIn.alias("addressbook", AddressBook.class);

		// AddressBook ab2 = (AddressBook) readIn.fromXML(xml);
		// System.out.println(ab2.GetAddressBookSize());
		// File Stuff

		try {
			FileInputStream fInput = new FileInputStream("E:\\test.txt");
			DataInputStream in = new DataInputStream(fInput);

			
			AddressBook ab3 = (AddressBook) readIn.fromXML(in);
			
			
			System.out.println("Address book has: " + ab3.ab.size()
					+ " entries");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static String getRandomName(boolean first) {
		int i = r.nextInt(31);
		if (first) {
			return FirstNameList[i];

		} else
			return LastNameList[i];

	}

}
